﻿using Reticulum.User;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reticulum.Communication
{
    public partial class ShareFile : Form
    {
        
        public ShareFile(string MyFriendsName)
        {
            InitializeComponent();
            UserID = MyFriendsName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ShareFile_Load(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();//清楚所有项,避免重复！

            string sql = string.Format("select (f.FlieName + f.FlieSuffix) 文件名,f.FlieGetTime 上传日期,cast((FlieLength/1024/1024)as decimal(20,3)) 文件大小,f.FlieSuffix 文件类型 " +
                "from FlieInfo f,Users u where f.UserID = u.UserID and u.UserID = '{0}';", ThisMyInfo.UserID);

            while (DBHelps.Select(sql).Read())
            {
                //创建ListViewItem对象，并设置Text属性
                ListViewItem item1 = new ListViewItem(DBHelps.Select(sql)["文件名"].ToString());

                //向ListViewItem对象中依次添加子项
                item1.SubItems.Add(DBHelps.Select(sql)["上传日期"].ToString());
                item1.SubItems.Add(DBHelps.Select(sql)["文件大小"].ToString());

                //向ListView控件中添加项
                this.listView1.Items.Add(item1);

                //设置该文件图标
                string Suffix = (DBHelps.Select(sql)["文件类型"].ToString());
                item1.ImageIndex = IfSuffix.IfSuffixs(Suffix);

                //item1.SubItems.

            }
            DBHelps.Select(sql).Close();
            DBHelps.reader = null;
        }

        private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        public static string FlieNames;// 文件名
        public static string FlieLength;// 文件大小
        public static string FlieRoute;// 文件路径（全路径）
        public static string FlieSuffix;// 文件后缀名
        public static string FlieGetTime;//--上传日期
        public static string UserID;// 用户ID，关联用户文件
        public static string FlieID;//文件ID
        public static string FileDownName;// 上传后文件名
        public static string original_File_name;// 原文件名


        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            string FileText = listView1.SelectedItems[0].Text;
            var DML = string.Format("select * from FlieInfo where original_File_name='{0}'", FileText);
            SqlDataReader reader = DBHelps.Select(DML);
            if (reader.Read())
            {
                FlieNames = reader["FlieName"].ToString();//文件名
                FlieLength = reader["FlieLength"].ToString();//文件大小
                FlieRoute = reader["FlieRoute"].ToString();//文件路径
                FlieSuffix = reader["FlieSuffix"].ToString();// 文件后缀名
                FlieGetTime = DateTime.Now.ToString();//当前日期
                FileDownName = reader["FileDownName"].ToString();
                original_File_name = reader["original_File_name"].ToString();
            }
            reader.Close();
            DBHelps.reader = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //UserFrom.MyFriendsName
                var DML = string.Format("insert into FlieInfo(FlieName,FlieLength,FlieRoute,FlieSuffix,FlieGetTime,UserID,FileDownName,original_File_name)" +
                "values('{0}', {1}, '{2}', '{3}', '{4}', {5}, '{6}', '{7}')", FlieNames, FlieLength, FlieRoute, FlieSuffix, FlieGetTime, UserID, FileDownName, original_File_name);
                if (DBHelps.Command(DML) == false)
                {
                    MessageBox.Show("发送失败");
                }
                else
                {

                    MessageBox.Show("你分享了："+ original_File_name);
                    //给好友发送提示消息
                    Loads.client.texMsg.Text= "向你分享了：" + original_File_name;
                    Loads.client.butsendMsg_Click(sender, e);

                    //Client.userFrom.texMsg.Text = "向你分享了：" + original_File_name;
                    //Client.userFrom.butsendMsg_Click();

                    this.Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("用户不在线");
            }
        }
        //无边框拖动窗体-----------------------------------------------
        [DllImport("user32.dll")]//*********************拖动无窗体的控件  需要引用
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int W = 0x0112;
        public const int S = 0xF010;
        public const int H = 0x0002;
        private void ShareFile_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, W, S + H, 0);//*********************调用移动无窗体控件函数
        }
    }
}
